package com.loveprogrammer.eternity.skill.support.skills;

import com.loveprogrammer.eternity.common.Tuple;
import com.loveprogrammer.eternity.enums.AttackTypeEnum;
import com.loveprogrammer.eternity.enums.WeaponTypeEnum;
import com.loveprogrammer.eternity.model.Character;
import com.loveprogrammer.eternity.model.Monster;
import com.loveprogrammer.eternity.model.attack.AttackEntity;
import com.loveprogrammer.eternity.model.attack.AttackResult;
import com.loveprogrammer.eternity.skill.support.states.ArmourReductionState;
import com.loveprogrammer.eternity.skill.support.states.BurnState;
import com.loveprogrammer.eternity.skill.support.states.LifeCurseState;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0.0
 * @description: 灾祸
 * @author: eric
 * @date: 2023-03-14 20:06
 **/
public class SkillDisaster extends AbstractSkill{

    public SkillDisaster(int id, int baseInterval, int level, int order, int maxLevel, String name, String desc) {
        super(id, baseInterval, level, order, maxLevel, name, desc);
    }

    @Override
    public boolean check(Character character) {
        if(character instanceof Monster){
            return true;
        }
        // 检查是否持有杖类武器
        return checkWeaponType(character, WeaponTypeEnum.WAND);
    }

    @Override
    public int opportunityPassive() {
        return 0;
    }

    @Override
    public boolean checkLevelUp(int characterLevel) {
        // 每5级升一级
        if(characterLevel % 5 == 0){
            return true;
        }
        return false;
    }

    @Override
    protected Tuple<String, List<Character>> skillLaunch(Character character, List<? extends Character> attack, List<? extends Character> defences) {
        Tuple<String, List<Character>> result = new Tuple<>();

        List<Character> characters = new ArrayList<>();
        // 按照血量排序，攻击血量最高的
        Character defence = findSingleAttacker(defences,false, true);

        // 首先是普通攻击
        StringBuilder builder = new StringBuilder();
        builder.append(character.getName()).append("(").append(character.getCurrentHp()).append("/")
                .append(character.getHpMax()).append(")").append(" 使用技能[")
                .append(getName()).append(" ").append(getLevel()).append("级】攻击了 ").append(defence.getName());

        int power = getHarm(character);
        boolean crit = critCheck(character);
        if (crit) {
            power = critPower(power);;
        }
        AttackResult attackResult = defence.defence(new AttackEntity(this, power, AttackTypeEnum.FLAME));
        computeHarm(builder,crit,defence,attackResult, character, attack, defences);

        double reduce = getSecondHarmDouble(character);
        // 生命诅咒伤害8秒钟
        LifeCurseState lifeCurseState = new LifeCurseState(defence,defences,reduce);
        boolean state = defence.acquireState(lifeCurseState, 8);
        // 施展成功 启动一个定时器，5秒钟
        if(state){
            setTimeTask(lifeCurseState,8);
            characters.add(defence);
            builder.append(defence.getName()).append("附加生命诅咒伤害;\n");
        }
        result.setValue(characters);
        result.setKey(builder.toString());
        return result;
    }

    @Override
    protected int computeHarm(Character character) {
        return (int) (character.getMagic() * 0.3 * ((120 + getLevel() * 0.5) / 100));
    }

    @Override
    protected int computeSecondHarm(Character character) {
        return 1;
    }
}
